'use strict';

const Controller = require('egg').Controller;

const createRule = {
  username: 'string',
  password: 'string',
};

class UserController extends Controller {
  async create() {
    const { ctx } = this;
    const { sys } = ctx.service;
    ctx.validate(createRule);
    try {
      const data = await sys.user.login(ctx.request.body);
      const { username, authToken, info } = data;
      ctx.set('Access-Token', `${authToken}`);
      ctx.set('Access-Control-Expose-Headers', 'Access-Token');
      ctx.success({
        data: { username, info },
      });
    } catch (error) {
      ctx.error({ detail: { type: 'account', message: error.message } });
    }
  }

  async destroy() {
    const ctx = this.ctx;
    const { sys } = ctx.service;
    const data = await sys.user.destroy(ctx.request.body);
    ctx.success({ data });
  }
}

module.exports = UserController;
